package com.java.learning.demo.common;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class ResultData<T> {
    public static final ResultData<String> SUCCESS=new ResultData<>(ReturnCode.SUCCESS);
    public static final String SUCCESS_MSG = "success";
    /**
     *
     */
    private static final long serialVersionUID = -9218871232480362373L;

    @ApiModelProperty(value = "请求返回时间", required = true)
    private long timestamp = System.currentTimeMillis();

    @ApiModelProperty(value = "状态码", required = true)
    private int code = ReturnCode.SUCCESS.getCode();

    @ApiModelProperty(value = "响应描述", required = true)
    private String message;

    @ApiModelProperty(value = "返回数据", required = true)
    private T data;

    protected ResultData(ReturnCode returnCode) {
        this(returnCode.getCode(),returnCode.getDescription());
    }


    protected ResultData(int code, String message) {
        this.code = code;
        this.message = message;
    }

    protected ResultData(ReturnCode returnCode, T data) {
        this(returnCode);
        this.data = data;
    }

    protected ResultData(int code, String message, T data) {
        this(code, message);
        this.data = data;
    }

    /**
     * 是否成功对象
     *
     * @param resultData
     * @return
     */
    public static <T> boolean isSuccess(ResultData<T> resultData) {
        return resultData != null && ReturnCode.SUCCESS.equals(resultData.getCode());
    }

    /**
     * 是否成功且有有效数据返回
     *
     * @param resultData
     * @return
     */
    public static <T> boolean isSuccessWithData(ResultData<T> resultData) {
        return isSuccess(resultData) && resultData != null;
    }

    /**
     * 带数据的成功实例
     *
     * @param data
     * @return
     */
    public static <T> ResultData<T> newInstance(T data) {
        return newInstance(ReturnCode.SUCCESS, data);
    }

    /**
     * 指定错误码、消息返回实例
     *
     * @param code
     * @param message
     * @return
     */
    public static <T> ResultData<T> newInstance(int code, String message) {
        return new ResultData<>(code, message);
    }
    public static <T> ResultData<T> newInstance(ReturnCode returnCode,T data) {
        return new ResultData<>(returnCode,data);
    }

    /**
     * 自定义实例
     *
     * @param code
     * @param message
     * @param data
     * @return
     */
    public static <T> ResultData<T> newInstance(int code, String message, T data) {
        return new ResultData<>(code, message, data);
    }

    /**
     * 检查返回错误消息
     *
     * @param resultData
     * @param <T>
     * @return
     */
    public static <T> String errorInfo(ResultData<T> resultData) {
        if (resultData == null) {
            return "Result data is null";
        } else if (!isSuccess(resultData)) {
            return resultData.toString();
        } else {
            return "ResultData is successfully returned.";
        }
    }

    @Override
    public String toString() {
        return "ResultData{" +
                "timestamp=" + timestamp +
                ", code='" + code + '\'' +
                ", message='" + message + '\'' +
                ", data=" + (data == null ? "" : data.toString()) +
                '}';
    }
}
